package cn;

/**
 * Demo is demo
 * <p/>
 * User: erhu
 * Date: 13-11-6
 * Time: 下午5:35
 */
public class Demo {

    public static void main(String[] args) {
        int[] data = new int[]{34, 516, 9, 123, 56, 86, 3769, 3450, 129, 3, 234};
        insertSortFaster(data);
        for (int d : data) {
            System.out.print(d + " ");
        }
    }

    private static void insertSort(int[] _array) {
        if (_array == null)
            return;

        int length = _array.length;
        for (int i = 1; i < length; i++) {
            for (int j = i; j > 0; j--) {
                if (j - 1 >= 0 && _array[j] < _array[j - 1]) {
                    int tmp = _array[j];
                    _array[j] = _array[j - 1];
                    _array[j - 1] = tmp;
                }
            }
        }
    }

    private static void insertSortFaster(int[] _array) {
        if (_array == null)
            return;

        int length = _array.length;
        for (int i = 1; i < length; i++) {
            int t_data = _array[i];

            int j = i;
            for (; j - 1 >= 0 && t_data < _array[j - 1]; j--) {
                _array[j] = _array[j - 1];
            }
            _array[j] = t_data;
        }
    }
}
